<template>
  <div class="login-container">
    <div class="login_img"></div>
    <div class="common-layout">
      <div class="top">
        <div class="header">
          <img alt="logo" class="logo" src="../../assets/images/logo.jpg" />

          <span class="title"> 工业云综合服务平台 </span>
        </div>
        <div class="desc">工业企业综合服务平台，为您的企业注入新活力。</div>
      </div>
      <div class="login">
        <a-form :form="form">
          <a-tabs
            size="large"
            :tabBarStyle="{ textAlign: 'center' }"
            style="padding: 0 2px"
          >
            <a-tab-pane tab="账户密码登录" key="1">
              <a-alert
                type="error"
                :closable="true"
                v-show="error"
                :message="error"
                showIcon
                style="margin-bottom: 24px"
              />
              <a-form-item>
                <a-input
                  autocomplete="autocomplete"
                  size="large"
                  placeholder="请输入账户名"
                  v-decorator="[
                    'username',
                    {
                      rules: [
                        {
                          required: true,
                          message: '请输入账户名',
                          whitespace: true,
                        },
                      ],
                    },
                  ]"
                >
                  <a-icon slot="prefix" type="user" />
                </a-input>
              </a-form-item>
              <a-form-item>
                <a-input-password
                  size="large"
                  placeholder="请输入密码"
                  autocomplete="autocomplete"
                  :visibilityToggle="true"
                  v-decorator="[
                    'password',
                    {
                      rules: [
                        {
                          required: true,
                          message: '请输入密码',
                          whitespace: true,
                        },
                      ],
                    },
                  ]"
                >
                  <a-icon slot="prefix" type="lock" />
                </a-input-password>
              </a-form-item>
            </a-tab-pane>
            <!-- <a-tab-pane tab="手机号登录" key="2">
              <a-form-item>
                <a-input size="large" placeholder="mobile number">
                  <a-icon slot="prefix" type="mobile" />
                </a-input>
              </a-form-item>
              <a-form-item>
                <a-row :gutter="8" style="margin: 0 -4px">
                  <a-col :span="16">
                    <a-input size="large" placeholder="captcha">
                      <a-icon slot="prefix" type="mail" />
                    </a-input>
                  </a-col>
                  <a-col :span="8" style="padding-left: 4px">
                    <a-button style="width: 100%" class="captcha-button" size="large">获取验证码</a-button>
                  </a-col>
                </a-row>
              </a-form-item>
            </a-tab-pane> -->
          </a-tabs>
          <!-- <div>
                    <a-checkbox :checked="true">自动登录</a-checkbox>
                    <a style="float: right">忘记密码</a>
                </div> -->
          <a-form-item>
            <a-button
              :loading="logging"
              style="width: 100%; margin-top: 24px"
              size="large"
              @click="onSubmit"
              type="primary"
              >登录</a-button
            >
          </a-form-item>
          <div class="footer">
            <!-- 其他登录方式
                    <a-icon class="icon" type="alipay-circle" />-->
            <a-button
              style="color: gray"
              type="link"
              html-type="submit"
              @click="$router.back()"
            >
              返回
            </a-button>
            <router-link style="float: right" to="/register"
              >注册账户</router-link
            >
          </div>
        </a-form>
      </div>
    </div>
  </div>
</template>

<script>
// import CommonLayout from '@/layouts/CommonLayout'
// import { login, getRoutesConfig } from '@/services/user'
// import { setAuthorization } from '@/utils/request'
// import { loadRoutes } from '@/utils/routerUtil'
// import { mapMutations } from 'vuex'
import { preLogin } from "../../apis/login";
import { getCompaynInfo } from "../../apis/com/company";
import {
  setToken,
  setCookiesUserInfo,
  getToken,
  setIsSign,
  removeToken,
  removeCookies,
} from "@/utils/auth";
import { encrypt } from '@/utils/jsencrypt'
export default {
  name: "Login",
  components: {},
  data() {
    return {
      logging: false,
      error: "",
      form: this.$form.createForm(this),
    };
  },
  computed: {},
  methods: {
    /**
     * 登录
     */
    onSubmit() {
      console.log("submit");
      this.logging = true;
      this.form.validateFields(async (err, values) => {
        if (!err) {
          values.password = encrypt(values.password);
          const res = await preLogin(values);
          const { data, code, msg } = res;
          if (code === 200) {
            const companyId = data.userInfo.sysUser.companyId;
            if (!companyId) {
              this.logging = false;
              return this.$message.error("您还未绑定企业，请联系管理员！");
            }

            const token = data.access_token;
            // const expires_in = data.expires_in
            const userInfo = data.userInfo.sysUser;
            setToken(token);
            setCookiesUserInfo(userInfo);
            const { data: company } = await getCompaynInfo(companyId);
            if (company.delFlag == 2) {
              this.logging = false;
              return this.$message.error("你所在的企业不存在或者已被删除！");
            }
            this.$message.success("登录成功");
            this.logging = false;
            this.$router.push("/home");

            return;
          }
          this.logging = false;
          this.$message.error(msg);
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login-container {
  display: flex;

  .login_img {
    background-image: url(../../assets/images/login-background.ca712ea7.jpg);
    height: 100vh;
    width: 70%;
    background-size: 100%;
    background-repeat: no-repeat;
  }
}

.common-layout {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -ms-flex-direction: column;
  flex-direction: column;
  height: 100vh;
  overflow: auto;
  background-color: #fff;
  // background-image: url(../../assets/images/login-background.ca712ea7.jpg);
  background-repeat: no-repeat;
  background-position-x: center;
  background-position-y: 110px;
  background-size: 100%;
  justify-content: center;
  align-items: flex-end;
  padding: 0 20px;
  // display: inline-block;
  align-items: center;
  justify-content: center;
  width: 30%;

  .top {
    text-align: center;
    width: 368px;

    .header {
      height: 44px;
      line-height: 44px;
      display: flex;
      align-items: center;

      a {
        text-decoration: none;
      }

      .logo {
        height: 46px;
        // vertical-align: center;
        margin-right: 10px;
        margin-top: 4px;
      }

      .title {
        font-size: 33px;
        color: rgba(0, 0, 0, 0.85);
        font-family: "Myriad Pro", "Helvetica Neue", Arial, Helvetica,
          sans-serif;
        font-weight: 600;
        position: relative;
        top: 2px;
      }
    }

    .desc {
      font-size: 14px;
      color: rgba(0, 0, 0, 0.45);
      margin-top: 12px;
      margin-bottom: 40px;
    }
  }

  .login {
    width: 368px;
    // margin: 0 auto;

    @media screen and (max-width: 576px) {
      width: 95%;
    }

    @media screen and (max-width: 320px) {
      .captcha-button {
        font-size: 14px;
      }
    }

    .icon {
      font-size: 24px;
      color: rgba(0, 0, 0, 0.45);
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;

      &:hover {
        color: @primary-color;
      }
    }
  }
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
